BEGIN TRANSACTION;
BEGIN TRY
    -- 检查库存并锁定行
    DECLARE @Stock INT;
    SELECT @Stock = Stock 
    FROM Products WITH (UPDLOCK) 
    WHERE ProductID = 100;

    IF @Stock > 0
    BEGIN
        UPDATE Products SET Stock = Stock - 1 WHERE ProductID = 100;
        COMMIT TRANSACTION;
    END
    ELSE
    BEGIN
        ROLLBACK TRANSACTION;
        RAISERROR('库存不足', 16, 1);
    END;
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION;
    THROW;
END CATCH;
